{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Create MBTiles from vector type data\n",
    "\n",
    "This notebook transforms vector type data into `MBTiles` using tippecanoe.\n",
    "\n",
    "## Table of Contents\n",
    "### [Python libraries](#libraries)\n",
    "### [Utils](#utils)\n",
    "- **[create_mbtiles](#create_mbtiles)**\n",
    "\n",
    "### [Read data](#carto)\n",
    "- **[Indigenous Territories](#indigenous_territories)**\n",
    "- **[Land Rights](#land_rights)**\n",
    "- **[Major Mineral Deposits](#mineral_deposits)**\n",
    "\n",
    "### [Create `MBTiles`](#create_mbtiles_2)\n",
    "### [Show `MBTiles` in our localhost](#show_mbtiles)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<a id='libraries'></a>\n",
    "### Python libraries"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import json\n",
    "import requests\n",
    "import geopandas as gpd\n",
    "import subprocess"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<a id='utils'></a>\n",
    "### Utils"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<a id='create_mbtiles'></a>\n",
    "**create_mbtiles**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "def create_mbtiles(source_path, dest_path, layer_name, opts=\"-zg --drop-densest-as-needed --extend-zooms-if-still-dropping --force --read-parallel\"):\n",
    "    \"\"\"\n",
    "    Use tippecanoe to create a MBTILE at dest_path from source_path.\n",
    "    layer_name is used for the name of the layer in the MBTILE.\n",
    "    Regex file path (/*.geojson) is supported for source_path.\n",
    "    \"\"\"\n",
    "    cmd = f\"tippecanoe -o {dest_path} -l {layer_name} {opts} {source_path}\"\n",
    "    print(f\"Processing: {cmd}\")\n",
    "    r = subprocess.call(cmd, shell=True)\n",
    "    if r == 0:\n",
    "        print(\"Task created\")\n",
    "    else:\n",
    "        print(\"Task failed\")\n",
    "    print(\"Finished processing\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<a id='carto'></a>\n",
    "### Read data "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<a id='indigenous_territories'></a>\n",
    "**[Indigenous Territories](https://native-land.ca/)**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "url = 'https://native-land.ca/coordinates/indigenousTerritories.json'\n",
    "\n",
    "data = requests.get(url).json()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Save data as `JSON`**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "with open('../data/mbtiles/Indigenous_Territories.json', 'w') as fp:\n",
    "    json.dump(data, fp)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<a id='land_rights'></a>\n",
    "**[Land Rights](http://data.globalforestwatch.org/datasets/e22b22b3525c46f3b35548daf6289903_1/data)**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>country</th>\n",
       "      <th>name</th>\n",
       "      <th>legal_term</th>\n",
       "      <th>legal_reco</th>\n",
       "      <th>area_ha</th>\n",
       "      <th>source</th>\n",
       "      <th>last_updat</th>\n",
       "      <th>gfwid</th>\n",
       "      <th>st_area_sh</th>\n",
       "      <th>st_length_</th>\n",
       "      <th>geometry</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>CAN</td>\n",
       "      <td>ENGLISH RIVER HAULTAIN LAKE I.R. 192K</td>\n",
       "      <td>Indian Reserve</td>\n",
       "      <td>Y</td>\n",
       "      <td>201.288027174093</td>\n",
       "      <td>Government of Canada, Natural Resources Canada...</td>\n",
       "      <td>2014</td>\n",
       "      <td>c531a195-09d5-449c-84fb-129d7bb77901</td>\n",
       "      <td>6.703042e+06</td>\n",
       "      <td>11842.772655</td>\n",
       "      <td>POLYGON ((-11833624.371 7725300.176, -11832861...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>CAN</td>\n",
       "      <td>ELAK DASE I.R. 192A</td>\n",
       "      <td>Indian Reserve</td>\n",
       "      <td>Y</td>\n",
       "      <td>1397.67492142709</td>\n",
       "      <td>Government of Canada, Natural Resources Canada...</td>\n",
       "      <td>2014</td>\n",
       "      <td>47f27ab3-f33b-407a-91dc-39667821c216</td>\n",
       "      <td>4.429801e+07</td>\n",
       "      <td>40679.063996</td>\n",
       "      <td>POLYGON ((-11887246.473 7528355.931, -11887244...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>CAN</td>\n",
       "      <td>MIN-A-HE-QUO-SIS INDIAN RESERVE NO. 116A</td>\n",
       "      <td>Indian Reserve</td>\n",
       "      <td>Y</td>\n",
       "      <td>260.538624047784</td>\n",
       "      <td>Government of Canada, Natural Resources Canada...</td>\n",
       "      <td>2014</td>\n",
       "      <td>0c445e83-c893-4fb3-9efb-efd0519a3df6</td>\n",
       "      <td>7.207503e+06</td>\n",
       "      <td>10749.287035</td>\n",
       "      <td>POLYGON ((-12139665.105 6999456.605, -12138323...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>CAN</td>\n",
       "      <td>TSU TUE 196G</td>\n",
       "      <td>Indian Reserve</td>\n",
       "      <td>Y</td>\n",
       "      <td>42.7328901923613</td>\n",
       "      <td>Government of Canada, Natural Resources Canada...</td>\n",
       "      <td>2014</td>\n",
       "      <td>349f8363-39ed-480b-bfd7-4228f5f3f4f1</td>\n",
       "      <td>1.698421e+06</td>\n",
       "      <td>5973.105602</td>\n",
       "      <td>POLYGON ((-12307276.499 8389447.707, -12307276...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>CAN</td>\n",
       "      <td>WHITE FISH LAKE 128</td>\n",
       "      <td>Indian Reserve</td>\n",
       "      <td>Y</td>\n",
       "      <td>6156.88101888983</td>\n",
       "      <td>Government of Canada, Natural Resources Canada...</td>\n",
       "      <td>2014</td>\n",
       "      <td>502a3edf-d25c-4121-863f-2a17dd1ab4d4</td>\n",
       "      <td>1.805401e+08</td>\n",
       "      <td>100897.806092</td>\n",
       "      <td>POLYGON ((-12445854.141 7221022.378, -12445888...</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  country                                      name      legal_term  \\\n",
       "0     CAN     ENGLISH RIVER HAULTAIN LAKE I.R. 192K  Indian Reserve   \n",
       "1     CAN                       ELAK DASE I.R. 192A  Indian Reserve   \n",
       "2     CAN  MIN-A-HE-QUO-SIS INDIAN RESERVE NO. 116A  Indian Reserve   \n",
       "3     CAN                              TSU TUE 196G  Indian Reserve   \n",
       "4     CAN                       WHITE FISH LAKE 128  Indian Reserve   \n",
       "\n",
       "  legal_reco           area_ha  \\\n",
       "0          Y  201.288027174093   \n",
       "1          Y  1397.67492142709   \n",
       "2          Y  260.538624047784   \n",
       "3          Y  42.7328901923613   \n",
       "4          Y  6156.88101888983   \n",
       "\n",
       "                                              source last_updat  \\\n",
       "0  Government of Canada, Natural Resources Canada...       2014   \n",
       "1  Government of Canada, Natural Resources Canada...       2014   \n",
       "2  Government of Canada, Natural Resources Canada...       2014   \n",
       "3  Government of Canada, Natural Resources Canada...       2014   \n",
       "4  Government of Canada, Natural Resources Canada...       2014   \n",
       "\n",
       "                                  gfwid    st_area_sh     st_length_  \\\n",
       "0  c531a195-09d5-449c-84fb-129d7bb77901  6.703042e+06   11842.772655   \n",
       "1  47f27ab3-f33b-407a-91dc-39667821c216  4.429801e+07   40679.063996   \n",
       "2  0c445e83-c893-4fb3-9efb-efd0519a3df6  7.207503e+06   10749.287035   \n",
       "3  349f8363-39ed-480b-bfd7-4228f5f3f4f1  1.698421e+06    5973.105602   \n",
       "4  502a3edf-d25c-4121-863f-2a17dd1ab4d4  1.805401e+08  100897.806092   \n",
       "\n",
       "                                            geometry  \n",
       "0  POLYGON ((-11833624.371 7725300.176, -11832861...  \n",
       "1  POLYGON ((-11887246.473 7528355.931, -11887244...  \n",
       "2  POLYGON ((-12139665.105 6999456.605, -12138323...  \n",
       "3  POLYGON ((-12307276.499 8389447.707, -12307276...  \n",
       "4  POLYGON ((-12445854.141 7221022.378, -12445888...  "
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = gpd.read_file('../data/mbtiles/gfw_land_rights/gfw_land_rights.shp')\n",
    "data['geometry'] = data['geometry'].apply(lambda x: x.simplify(0.0075))\n",
    "data.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Geographic 2D CRS: EPSG:4326>\n",
       "Name: WGS 84\n",
       "Axis Info [ellipsoidal]:\n",
       "- Lat[north]: Geodetic latitude (degree)\n",
       "- Lon[east]: Geodetic longitude (degree)\n",
       "Area of Use:\n",
       "- name: World\n",
       "- bounds: (-180.0, -90.0, 180.0, 90.0)\n",
       "Datum: World Geodetic System 1984\n",
       "- Ellipsoid: WGS 84\n",
       "- Prime Meridian: Greenwich"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = data.to_crs(\"EPSG:4326\")\n",
    "data.crs"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Save data as `JSON`**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [],
   "source": [
    "data.to_file('../data/mbtiles/Land_Rights.json', driver=\"GeoJSON\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<a id='mineral_deposits'></a>\n",
    "**[Major Mineral Deposits](https://mrdata.usgs.gov/major-deposits/)**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>REC_ID</th>\n",
       "      <th>DEP_NAME</th>\n",
       "      <th>COUNTRY</th>\n",
       "      <th>STATE</th>\n",
       "      <th>COMMODITY</th>\n",
       "      <th>DEP_TYPE</th>\n",
       "      <th>DETAIL</th>\n",
       "      <th>MODEL</th>\n",
       "      <th>DEP_MODEL</th>\n",
       "      <th>CITATION</th>\n",
       "      <th>CATEGORY</th>\n",
       "      <th>URL</th>\n",
       "      <th>geometry</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>325</td>\n",
       "      <td>Wando</td>\n",
       "      <td>Korean Peninsula</td>\n",
       "      <td>None</td>\n",
       "      <td>Pyrophyllite</td>\n",
       "      <td>Hydrothermal</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>O'Driscoll, 1993</td>\n",
       "      <td>None</td>\n",
       "      <td>https://mrdata.usgs.gov/major-deposits/show-of...</td>\n",
       "      <td>POINT (126.68330 34.35000)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>52</td>\n",
       "      <td>Georgina Basin</td>\n",
       "      <td>Australia</td>\n",
       "      <td>None</td>\n",
       "      <td>Phosphorous</td>\n",
       "      <td>Sedimentary</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>Industrial Minerals, 1998</td>\n",
       "      <td>None</td>\n",
       "      <td>https://mrdata.usgs.gov/major-deposits/show-of...</td>\n",
       "      <td>POINT (139.96670 -21.88330)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1880</td>\n",
       "      <td>Wheal Jane</td>\n",
       "      <td>United Kingdom</td>\n",
       "      <td>None</td>\n",
       "      <td>Tin</td>\n",
       "      <td>Hydrothermal</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>https://mrdata.usgs.gov/major-deposits/show-of...</td>\n",
       "      <td>POINT (-5.20000 50.23333)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1979</td>\n",
       "      <td>Kaldygajtinskoe</td>\n",
       "      <td>Kazakhstan</td>\n",
       "      <td>None</td>\n",
       "      <td>Halite</td>\n",
       "      <td>Sedimentary</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>https://mrdata.usgs.gov/major-deposits/show-of...</td>\n",
       "      <td>POINT (53.60583 49.80639)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2548</td>\n",
       "      <td>Imiter</td>\n",
       "      <td>Morocco</td>\n",
       "      <td>None</td>\n",
       "      <td>Silver</td>\n",
       "      <td>Hydrothermal</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>https://mrdata.usgs.gov/major-deposits/show-of...</td>\n",
       "      <td>POINT (-5.83360 31.35000)</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   REC_ID         DEP_NAME           COUNTRY STATE     COMMODITY  \\\n",
       "0     325            Wando  Korean Peninsula  None  Pyrophyllite   \n",
       "1      52   Georgina Basin         Australia  None   Phosphorous   \n",
       "2    1880       Wheal Jane    United Kingdom  None           Tin   \n",
       "3    1979  Kaldygajtinskoe        Kazakhstan  None        Halite   \n",
       "4    2548           Imiter           Morocco  None        Silver   \n",
       "\n",
       "       DEP_TYPE DETAIL MODEL DEP_MODEL                   CITATION CATEGORY  \\\n",
       "0  Hydrothermal   None  None      None           O'Driscoll, 1993     None   \n",
       "1   Sedimentary   None  None      None  Industrial Minerals, 1998     None   \n",
       "2  Hydrothermal   None  None      None                       None     None   \n",
       "3   Sedimentary   None  None      None                       None     None   \n",
       "4  Hydrothermal   None  None      None                       None     None   \n",
       "\n",
       "                                                 URL  \\\n",
       "0  https://mrdata.usgs.gov/major-deposits/show-of...   \n",
       "1  https://mrdata.usgs.gov/major-deposits/show-of...   \n",
       "2  https://mrdata.usgs.gov/major-deposits/show-of...   \n",
       "3  https://mrdata.usgs.gov/major-deposits/show-of...   \n",
       "4  https://mrdata.usgs.gov/major-deposits/show-of...   \n",
       "\n",
       "                      geometry  \n",
       "0   POINT (126.68330 34.35000)  \n",
       "1  POINT (139.96670 -21.88330)  \n",
       "2    POINT (-5.20000 50.23333)  \n",
       "3    POINT (53.60583 49.80639)  \n",
       "4    POINT (-5.83360 31.35000)  "
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = gpd.read_file('../data/mbtiles/major_mineral_deposits/ofr20051294.shp')\n",
    "data.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Save data as `JSON`**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "data.to_file('../data/mbtiles/Major_Mineral_Deposits.json', driver=\"GeoJSON\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<a id='create_mbtiles_2'></a>\n",
    "### Create `MBTiles`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Processing: tippecanoe -o ../data/mbtiles/Land_Rights.mbtiles -l Land Rights -zg --drop-densest-as-needed --extend-zooms-if-still-dropping --force --read-parallel ../data/mbtiles/Land_Rights.json\n",
      "Task created\n",
      "Finished processing\n"
     ]
    }
   ],
   "source": [
    "layers = {\"Indigenous Territories\": 'Indigenous_Territories',\n",
    "         \"Land Rights\": 'Land_Rights',\n",
    "         \"Major Mineral Deposits\": 'Major_Mineral_Deposits'}\n",
    "\n",
    "for layer_name, file in layers.items():\n",
    "\n",
    "    source_path = f\"../data/mbtiles/{file}.json\"\n",
    "    dest_path = f\"../data/mbtiles/{file}.mbtiles\"\n",
    "    create_mbtiles(source_path, dest_path, layer_name, opts=\"-zg --drop-densest-as-needed --extend-zooms-if-still-dropping --force --read-parallel\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<a id='show_mbtiles'></a>\n",
    "### Show `MBTiles` in our localhost"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Listening on http://localhost:9000\n",
      "^C\n"
     ]
    }
   ],
   "source": [
    "!mbview --port 9000 ../data/mbtiles/Land_Rights.mbtiles"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
